<!DOCTYPE html>
<html>
<head>
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d890b1f16fb364253e79c5bb20225c3a";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?busuanzi_value_site_uv"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    <meta name="baidu-site-verification" content="FYMCShbUK8" />
    <meta name="baidu-site-verification" content="ZYRF7OxQRW" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    
    
    <link rel="canonical" href="https://hhardyy.com/2018/09/17/密码算法实验/">
    
    
    <title>密码算法实验 | 小方块 - hhardyy.com | 复杂的坑+归其原理+了解实现规则===解决？解决成功：加油解决成功;</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#958e93">
    
    
    <meta name="keywords" content="java,RSA,AES,HASH,网络安全">
    <meta name="description" content="很久很久很久，简直久以前的密码算法实验。。。">
<meta name="keywords" content="java,RSA,AES,HASH,网络安全">
<meta property="og:type" content="article">
<meta property="og:title" content="密码算法实验">
<meta property="og:url" content="http://yoursite.com/2018/09/17/密码算法实验/index.html">
<meta property="og:site_name" content="小方块 - hhardyy.com">
<meta property="og:description" content="很久很久很久，简直久以前的密码算法实验。。。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/images/javaSav/run.JPG">
<meta property="og:updated_time" content="2020-01-13T15:34:10.283Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="密码算法实验">
<meta name="twitter:description" content="很久很久很久，简直久以前的密码算法实验。。。">
<meta name="twitter:image" content="http://yoursite.com/images/javaSav/run.JPG">
    
        <link rel="alternate" type="application/atom+xml" title="小方块 - hhardyy.com" href="/atom.xml">
    
    <link rel="shortcut icon" href="/hardyfavicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/paulGraham.jpg)">
      <div class="brand" style="background-color:#4154b2">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">BingZhenhuang</h5>
          <a href="mailto:huangbingzhen@hhardyy.com" title="huangbingzhen@hhardyy.com" class="mail">huangbingzhen@hhardyy.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                所有文章
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/HHardyy" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://juejin.im/user/59a26f926fb9a02487553b04"  >
                <i class="icon icon-lg icon-pencil"></i>
                掘金-圳
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://segmentfault.com/u/hhardyy"  >
                <i class="icon icon-lg icon-comments"></i>
                Segmentfault
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://codepen.io/HHardyy/" target="_blank" >
                <i class="icon icon-lg icon-codepen"></i>
                Codepen
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/ZhenSolive/find.html" target="_blank" >
                <i class="icon icon-lg icon-globe"></i>
                原生直播间
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/hero/judge.html" target="_blank" >
                <i class="icon icon-lg icon-cloud"></i>
                原生小悟空
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/airPlay/HHardyy_PC.html" target="_blank" >
                <i class="icon icon-lg icon-camera"></i>
                原生飞机大战（PC）
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.freecodecamp.cn/hhardyy" target="_blank" >
                <i class="icon icon-lg icon-leaf"></i>
                Freecodecamp
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/友情链接"  >
                <i class="icon icon-lg icon-link"></i>
                友链
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">密码算法实验</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">密码算法实验</h1>
        <h5 class="subtitle">
            
                <time datetime="2018-09-17T09:07:33.000Z" itemprop="datePublished" class="page-time">
  2018-09-17
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#服务器启动，开启6666端口"><span class="post-toc-number">1.</span> <span class="post-toc-text">服务器启动，开启6666端口</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#客户机接入"><span class="post-toc-number">2.</span> <span class="post-toc-text">客户机接入</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#客户机构建窗体"><span class="post-toc-number">3.</span> <span class="post-toc-text">客户机构建窗体</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#服务器监听端口"><span class="post-toc-number">4.</span> <span class="post-toc-text">服务器监听端口</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#加密身份认证"><span class="post-toc-number">5.</span> <span class="post-toc-text">加密身份认证</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#解密认证"><span class="post-toc-number">6.</span> <span class="post-toc-text">解密认证</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#通信数据加密"><span class="post-toc-number">7.</span> <span class="post-toc-text">通信数据加密</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#客户机关闭连接"><span class="post-toc-number">8.</span> <span class="post-toc-text">客户机关闭连接</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#剑谱"><span class="post-toc-number">9.</span> <span class="post-toc-text">剑谱</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#保存秘钥的方法"><span class="post-toc-number">9.1.</span> <span class="post-toc-text">保存秘钥的方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#保存数据的方法"><span class="post-toc-number">9.2.</span> <span class="post-toc-text">保存数据的方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#读取数据的方法"><span class="post-toc-number">9.3.</span> <span class="post-toc-text">读取数据的方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#读取秘钥的方法"><span class="post-toc-number">9.4.</span> <span class="post-toc-text">读取秘钥的方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Coder-java"><span class="post-toc-number">9.5.</span> <span class="post-toc-text">Coder.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#AES-java"><span class="post-toc-number">9.6.</span> <span class="post-toc-text">AES.java</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Hash-java"><span class="post-toc-number">9.7.</span> <span class="post-toc-text">Hash.java</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#RSA-java"><span class="post-toc-number">10.</span> <span class="post-toc-text">RSA.java</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-密码算法实验"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">密码算法实验</h1>
        <div class="post-meta">
            <time class="post-time" title="2018-09-17 17:07:33" datetime="2018-09-17T09:07:33.000Z"  itemprop="datePublished">2018-09-17</time>

            


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>很久很久很久，简直久以前的密码算法实验。。。</p>
<p><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=1300819741&auto=0&height=66"></iframe><br><a id="more"></a></p>
<h3 id="服务器启动，开启6666端口"><a href="#服务器启动，开启6666端口" class="headerlink" title="服务器启动，开启6666端口"></a>服务器启动，开启6666端口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">		ss = <span class="keyword">new</span> ServerSocket(<span class="number">6666</span>);</div><div class="line">		started = <span class="keyword">true</span>;</div><div class="line">	&#125; <span class="keyword">catch</span> (BindException e) &#123;</div><div class="line">        System.out.println(<span class="string">"Sorry,Port in use!"</span>);</div><div class="line">        System.out.println(<span class="string">"Please turn off the program to restart the server!"</span>);</div><div class="line">        System.exit(<span class="number">0</span>);</div><div class="line">	&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">		e.printStackTrace();</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">try</span> &#123;</div><div class="line">		<span class="keyword">while</span> (started) &#123;</div><div class="line">		    	Socket s = ss.accept();</div><div class="line">		    	Client c = <span class="keyword">new</span> Client(s);</div><div class="line">		    	System.out.println(<span class="string">"a client connected!"</span>);</div><div class="line">			    <span class="keyword">new</span> Thread(c).start();</div><div class="line">			    clients.add(c);</div><div class="line">		    &#125;</div><div class="line">	    &#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">			e.printStackTrace();</div><div class="line">	&#125; <span class="keyword">finally</span> &#123;</div><div class="line">	<span class="keyword">try</span> &#123;</div><div class="line">		ss.close();</div><div class="line">	&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">		e.printStackTrace();</div><div class="line">		&#125;</div><div class="line">	&#125;</div></pre></td></tr></table></figure>
<h3 id="客户机接入"><a href="#客户机接入" class="headerlink" title="客户机接入"></a>客户机接入</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">		s = <span class="keyword">new</span> Socket(<span class="string">"localhost"</span>, <span class="number">6666</span>);</div><div class="line">		dos = <span class="keyword">new</span> DataOutputStream(s.getOutputStream());</div><div class="line">		dis = <span class="keyword">new</span> DataInputStream(s.getInputStream());</div><div class="line">		bConnected = <span class="keyword">true</span>;</div><div class="line">	&#125; <span class="keyword">catch</span> (UnknownHostException e) &#123;</div><div class="line">			e.printStackTrace();</div><div class="line">	&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">			e.printStackTrace();</div><div class="line">	&#125;</div></pre></td></tr></table></figure>
<h3 id="客户机构建窗体"><a href="#客户机构建窗体" class="headerlink" title="客户机构建窗体"></a>客户机构建窗体</h3><p>（比较简陋的项目，只是eclipse里面做个窗体来假装客户机）<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">setLocation(<span class="number">600</span>, <span class="number">300</span>);</div><div class="line">add(tfTxt, BorderLayout.SOUTH);</div><div class="line">add(taContent, BorderLayout.NORTH);</div><div class="line">pack();</div><div class="line"><span class="keyword">this</span>.addWindowListener(<span class="keyword">new</span> WindowAdapter() &#123;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">windowClosing</span><span class="params">(WindowEvent arg0)</span> </span>&#123;</div><div class="line">		disconnect();</div><div class="line">		System.exit(<span class="number">0</span>);</div><div class="line">	&#125;</div><div class="line">&#125;);</div><div class="line">tfTxt.addActionListener(<span class="keyword">new</span> TFListener());</div><div class="line">setVisible(<span class="keyword">true</span>);</div><div class="line">connect();</div><div class="line">tRecv.start();</div></pre></td></tr></table></figure></p>
<h3 id="服务器监听端口"><a href="#服务器监听端口" class="headerlink" title="服务器监听端口"></a>服务器监听端口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">	<span class="keyword">while</span> (started) &#123;</div><div class="line">		Socket s = ss.accept();</div><div class="line">		Client c = <span class="keyword">new</span> Client(s);</div><div class="line">		System.out.println(<span class="string">"a client connected!"</span>);</div><div class="line">		<span class="keyword">new</span> Thread(c).start();</div><div class="line">		clients.add(c);</div><div class="line">	&#125;</div><div class="line">&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">	e.printStackTrace();</div><div class="line">&#125; <span class="keyword">finally</span> &#123;</div><div class="line">	<span class="keyword">try</span> &#123;</div><div class="line">		ss.close();</div><div class="line">	&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">		<span class="comment">// TODO Auto-generated catch block</span></div><div class="line">		e.printStackTrace();</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="加密身份认证"><a href="#加密身份认证" class="headerlink" title="加密身份认证"></a>加密身份认证</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">String str1 = <span class="keyword">null</span>;</div><div class="line">Random random = <span class="keyword">new</span> Random();</div><div class="line"><span class="keyword">int</span> r = random.nextInt();</div><div class="line">String u = <span class="string">"hhardyy"</span>;</div><div class="line">String Verification_code = u + r;</div><div class="line">String Decryption_certification = <span class="keyword">null</span>;</div><div class="line">System.out.println(<span class="string">"发送的认证消息"</span> + u + r);</div><div class="line"><span class="comment">// 生成一对公钥</span></div><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">	Rsa rsa = <span class="keyword">new</span> Rsa();</div><div class="line">	KeyPair rsak = rsa.genKeyPair();</div><div class="line">	PrivateKey ks = rsak.getPrivate();<span class="comment">// 私钥</span></div><div class="line">	PublicKey kp = rsak.getPublic();<span class="comment">// 公钥</span></div><div class="line"></div><div class="line">	<span class="comment">// 用公钥把发送的时候生成的随机数进行加密发送</span></div><div class="line">	<span class="keyword">byte</span>[] byte_Verification_code = Verification_code.getBytes(<span class="string">"utf-8"</span>);</div><div class="line">	<span class="keyword">byte</span>[] er = rsa.encrypt(byte_Verification_code, kp);</div><div class="line">	<span class="comment">// 保存密钥</span></div><div class="line">	saveKey(ks, <span class="string">"key_Private.key"</span>);</div><div class="line">	saveData(er, <span class="string">"key_pubData.data"</span>);</div><div class="line"></div><div class="line">	str1 = <span class="keyword">new</span> String(er);</div><div class="line">	System.out.println(<span class="string">"服务器收到的客户机加密用户认证："</span> + str1);</div><div class="line">&#125; <span class="keyword">catch</span> (Exception e1) &#123;</div><div class="line">	<span class="comment">// TODO Auto-generated catch block</span></div><div class="line">	e1.printStackTrace();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="解密认证"><a href="#解密认证" class="headerlink" title="解密认证"></a>解密认证</h3><p>注意：这里判断两个值是否相等不能用==或者===，=更不行，因为==或者===对比的是值得路径等等，所以要用str1.equals(str2)<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">String DemsgString=<span class="keyword">null</span>;<span class="comment">//定义一个变量存储通信数据</span></div><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">	<span class="comment">// 解密start</span></div><div class="line">	Rsa rsa = <span class="keyword">new</span> Rsa();</div><div class="line">	PrivateKey privatekey = readKey(<span class="string">"key_Private.key"</span>); </div><div class="line">	<span class="keyword">byte</span>[] er = readData(<span class="string">"key_pubData.data"</span>); </div><div class="line">	<span class="keyword">byte</span>[] k = rsa.decrypt(er, privatekey); <span class="comment">//k = RSAdec(ek, ks)				</span></div><div class="line">    <span class="keyword">if</span> (Verification_code.equals(Decryption_certification)) &#123;</div><div class="line">        str2 = <span class="string">"认证成功，允许通信"</span>;	<span class="comment">//测试成功		</span></div><div class="line">	&#125; <span class="keyword">else</span> &#123;</div><div class="line">        str2 = <span class="string">"认证失败"</span>; <span class="comment">//测试失败</span></div><div class="line">		DemsgString=<span class="string">"身份认证失败"</span>;</div><div class="line">	&#125;</div><div class="line">&#125; <span class="keyword">catch</span> (Exception e2) &#123;</div><div class="line">	e2.printStackTrace();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="通信数据加密"><a href="#通信数据加密" class="headerlink" title="通信数据加密"></a>通信数据加密</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">	Aes aes = <span class="keyword">new</span> Aes();			</div><div class="line">	<span class="comment">//生成密钥，用口令来生成密钥</span></div><div class="line">	String pwd = <span class="string">"xiaofangkuai"</span>;</div><div class="line">	<span class="keyword">byte</span>[] aesKs = aes.genKey(pwd);</div><div class="line">	System.out.println(<span class="string">"通信内容："</span>+str);</div><div class="line">	<span class="keyword">byte</span>[] msgb=str.getBytes(<span class="string">"utf-8"</span>);</div><div class="line">	<span class="keyword">byte</span>[] em = aes.encrypt(msgb, aesKs);<span class="comment">//通信内容加密</span></div><div class="line">							</div><div class="line">	print(<span class="string">"AES口令生成秘钥Key:"</span>,aesKs);</div><div class="line">	System.out.println(<span class="string">""</span>);</div><div class="line">	String EemsgString = <span class="keyword">new</span> String(em, <span class="string">"UTF-8"</span>);</div><div class="line">	System.out.println(<span class="string">"加密之后："</span>+EemsgString);</div><div class="line">							</div><div class="line">	<span class="keyword">byte</span>[] m = aes.decrypt(em, aesKs);</div><div class="line">	DemsgString = <span class="keyword">new</span> String(m, <span class="string">"UTF-8"</span>);</div><div class="line">	System.out.println(<span class="string">"解密之后："</span>+m);</div><div class="line">	System.out.println(<span class="string">"转码："</span>+DemsgString);</div><div class="line">	&#125; <span class="keyword">catch</span> (Exception ec) &#123;</div><div class="line">		ec.printStackTrace();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="客户机关闭连接"><a href="#客户机关闭连接" class="headerlink" title="客户机关闭连接"></a>客户机关闭连接</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">		dos.close();</div><div class="line">		dis.close();</div><div class="line">		s.close();</div><div class="line">	&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">		e.printStackTrace();</div><div class="line">	&#125;</div><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">		bConnected = <span class="keyword">false</span>;</div><div class="line">		tRecv.join();</div><div class="line">	&#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</div><div class="line">		e.printStackTrace();</div><div class="line">	&#125; <span class="keyword">finally</span> &#123;</div><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">		dos.close();</div><div class="line">		dis.close();</div><div class="line">		s.close();</div><div class="line">	&#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">		e.printStackTrace();</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>例子比较简陋，看懂不懂没关系，主要看配图<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/javaSav/run.JPG" alt="。。。" title="">
                </div>
                <div class="image-caption">。。。</div>
            </figure></p>
<h3 id="剑谱"><a href="#剑谱" class="headerlink" title="剑谱"></a>剑谱</h3><h4 id="保存秘钥的方法"><a href="#保存秘钥的方法" class="headerlink" title="保存秘钥的方法"></a>保存秘钥的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">saveKey</span><span class="params">(Key key, String keyName)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">	FileOutputStream foskey = <span class="keyword">new</span> FileOutputStream(keyName);</div><div class="line">	ObjectOutputStream oos = <span class="keyword">new</span> ObjectOutputStream(foskey);</div><div class="line">	oos.writeObject(key);</div><div class="line">	oos.close();</div><div class="line">	foskey.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="保存数据的方法"><a href="#保存数据的方法" class="headerlink" title="保存数据的方法"></a>保存数据的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">saveData</span><span class="params">(<span class="keyword">byte</span>[] results, String dataName)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">	FileOutputStream fosData = <span class="keyword">new</span> FileOutputStream(dataName);</div><div class="line">	fosData.write(results);</div><div class="line">	fosData.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="读取数据的方法"><a href="#读取数据的方法" class="headerlink" title="读取数据的方法"></a>读取数据的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">byte</span>[] readData(String dataName) <span class="keyword">throws</span> Exception &#123;</div><div class="line">	FileInputStream fisDat = <span class="keyword">new</span> FileInputStream(dataName);</div><div class="line"></div><div class="line">	<span class="comment">// 读二进制数据</span></div><div class="line">	ByteArrayOutputStream arrayOutputStream = <span class="keyword">new</span> ByteArrayOutputStream();</div><div class="line">	<span class="keyword">int</span> len = <span class="number">0</span>;</div><div class="line">	<span class="keyword">byte</span>[] data = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</div><div class="line">	<span class="keyword">while</span> ((len = fisDat.read(data)) != -<span class="number">1</span>) &#123;</div><div class="line">		arrayOutputStream.write(data, <span class="number">0</span>, len);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">byte</span>[] result = arrayOutputStream.toByteArray();</div><div class="line">	arrayOutputStream.close();</div><div class="line"></div><div class="line">	fisDat.close();</div><div class="line">	<span class="keyword">return</span> result;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="读取秘钥的方法"><a href="#读取秘钥的方法" class="headerlink" title="读取秘钥的方法"></a>读取秘钥的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> PrivateKey <span class="title">readKey</span><span class="params">(String keyName)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">	FileInputStream fiskey = <span class="keyword">new</span> FileInputStream(keyName);</div><div class="line">	ObjectInputStream oiskey = <span class="keyword">new</span> ObjectInputStream(fiskey);</div><div class="line">	PrivateKey key = (PrivateKey) oiskey.readObject();</div><div class="line">	oiskey.close();</div><div class="line">	fiskey.close();</div><div class="line">	<span class="keyword">return</span> key;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="Coder-java"><a href="#Coder-java" class="headerlink" title="Coder.java"></a>Coder.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.util.Base64;</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Coder</span> </span>&#123;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">encode</span><span class="params">(<span class="keyword">byte</span>[] data)</span> </span>&#123;</div><div class="line">		Base64.Encoder ecd = Base64.getEncoder();</div><div class="line">		<span class="keyword">return</span> ecd.encodeToString(data);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">byte</span>[] decode(String str) &#123;</div><div class="line">		Base64.Decoder dcd = Base64.getDecoder();</div><div class="line">		<span class="keyword">return</span> dcd.decode(str);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="AES-java"><a href="#AES-java" class="headerlink" title="AES.java"></a>AES.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.security.SecureRandom;</div><div class="line"><span class="keyword">import</span> javax.crypto.Cipher;</div><div class="line"><span class="keyword">import</span> javax.crypto.KeyGenerator;</div><div class="line"><span class="keyword">import</span> javax.crypto.SecretKey;</div><div class="line"><span class="keyword">import</span> javax.crypto.spec.SecretKeySpec;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestAES</span> </span>&#123;</div><div class="line">	<span class="keyword">private</span> Cipher cipher;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="title">TestAES</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">		cipher=Cipher.getInstance(<span class="string">"AES"</span>);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">private</span> SecretKey <span class="title">getKey</span><span class="params">(<span class="keyword">byte</span>[] key)</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">		<span class="keyword">return</span> <span class="keyword">new</span> SecretKeySpec(key, <span class="string">"AES"</span>);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="keyword">public</span> <span class="keyword">byte</span>[] encrypt(<span class="keyword">byte</span>[] data, <span class="keyword">byte</span>[] key) <span class="keyword">throws</span> Exception&#123;</div><div class="line">		cipher.init(Cipher.ENCRYPT_MODE, getKey(key));</div><div class="line">		<span class="keyword">byte</span> [] encData=cipher.doFinal(data);</div><div class="line">		<span class="keyword">return</span> encData;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="keyword">public</span> <span class="keyword">byte</span>[] decrypt(<span class="keyword">byte</span>[] encData, <span class="keyword">byte</span>[] key) <span class="keyword">throws</span> Exception &#123;</div><div class="line">		cipher.init(Cipher.DECRYPT_MODE, getKey(key));</div><div class="line">		<span class="keyword">byte</span> [] data=cipher.doFinal(encData);</div><div class="line">		<span class="keyword">return</span> data;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="keyword">public</span> <span class="keyword">byte</span>[] genKey(String fact) <span class="keyword">throws</span> Exception&#123;</div><div class="line">		KeyGenerator keygen=KeyGenerator.getInstance(<span class="string">"AES"</span>);</div><div class="line">        keygen.init(<span class="number">128</span>, <span class="keyword">new</span> SecureRandom(fact.getBytes()));</div><div class="line">        SecretKey original_key=keygen.generateKey();</div><div class="line">        <span class="keyword">byte</span> [] key=original_key.getEncoded();</div><div class="line">        <span class="keyword">return</span> key;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">genKeyStr</span><span class="params">(String fact)</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        <span class="keyword">byte</span> [] key=genKey(fact);</div><div class="line">        String keyStr = Coder.encode(key);</div><div class="line">        <span class="keyword">return</span> keyStr;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">encrypt</span><span class="params">(String dataStr, String keyStr)</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">		<span class="keyword">byte</span>[] data = dataStr.getBytes();</div><div class="line">		<span class="keyword">byte</span>[] key = Coder.decode(keyStr);</div><div class="line">		<span class="keyword">byte</span>[] edata = encrypt(data,key);</div><div class="line">		<span class="keyword">return</span> Coder.encode(edata);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">decrypt</span><span class="params">(String encDataStr, String keyStr)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">		<span class="keyword">byte</span>[] edata = Coder.decode(encDataStr);</div><div class="line">		<span class="keyword">byte</span>[] key = Coder.decode(keyStr);</div><div class="line">		<span class="keyword">byte</span> [] data = decrypt(edata,key);</div><div class="line">		<span class="keyword">return</span> <span class="keyword">new</span> String(data);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">		<span class="keyword">try</span> &#123;</div><div class="line">			TestAES t = <span class="keyword">new</span> TestAES();</div><div class="line">			</div><div class="line">			<span class="comment">//生成密钥，用口令来生成密钥</span></div><div class="line">			String pwd = <span class="string">"123456"</span>;</div><div class="line">			<span class="keyword">byte</span>[] k = t.genKey(pwd);</div><div class="line">			</div><div class="line">			String msg = <span class="string">"Plain text 1"</span>;</div><div class="line">			<span class="keyword">byte</span>[] msgb=msg.getBytes();</div><div class="line">			</div><div class="line">			<span class="keyword">byte</span>[] emsgb = t.encrypt(msgb, k);</div><div class="line">			<span class="keyword">byte</span>[] dmsgb = t.decrypt(emsgb, k);</div><div class="line"></div><div class="line">			t.print(<span class="string">"Key"</span>,k);</div><div class="line">			t.print(<span class="string">"msgb"</span>, msgb);</div><div class="line">			t.print(<span class="string">"emsgb"</span>, emsgb);</div><div class="line">			t.print(<span class="string">"dmsgb"</span>, dmsgb);</div><div class="line"></div><div class="line">			System.out.println(<span class="string">"End"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">catch</span>(Exception ec) &#123;</div><div class="line">			ec.printStackTrace();</div><div class="line">		&#125;</div><div class="line"></div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">(String note, <span class="keyword">byte</span>[] bs)</span></span>&#123;</div><div class="line">		System.out.println(note+<span class="string">": "</span>+Coder.encode(bs));</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="Hash-java"><a href="#Hash-java" class="headerlink" title="Hash.java"></a>Hash.java</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.security.MessageDigest;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestHash</span> </span>&#123;</div><div class="line">	<span class="keyword">public</span> <span class="keyword">byte</span>[] getSha256(<span class="keyword">byte</span>[] data) <span class="keyword">throws</span> Exception&#123;</div><div class="line">		MessageDigest  md = MessageDigest.getInstance(<span class="string">"SHA-256"</span>);</div><div class="line">	    md.update(data);</div><div class="line">	    <span class="keyword">return</span> md.digest();</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">public</span> <span class="keyword">byte</span>[] getSha1(<span class="keyword">byte</span>[] data) <span class="keyword">throws</span> Exception</div><div class="line">	&#123;</div><div class="line">		MessageDigest messageDigest = MessageDigest.getInstance(<span class="string">"SHA-1"</span>);    </div><div class="line">		messageDigest.update(data);</div><div class="line">		<span class="keyword">return</span> messageDigest.digest();    </div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">		<span class="keyword">try</span> &#123;</div><div class="line">			TestHash t = <span class="keyword">new</span> TestHash();</div><div class="line">			</div><div class="line">			String msg = <span class="string">"a"</span>;</div><div class="line">			<span class="keyword">byte</span>[] msgb=msg.getBytes();</div><div class="line">			<span class="keyword">byte</span>[] d = t.getSha256(msgb);</div><div class="line">			</div><div class="line">			t.print(<span class="string">"msgb"</span>, msgb);</div><div class="line">			t.print(<span class="string">"Dig"</span>, d);</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">catch</span>(Exception ec) &#123;</div><div class="line">			ec.printStackTrace();</div><div class="line">		&#125;</div><div class="line"></div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">(String note, <span class="keyword">byte</span>[] bs)</span></span>&#123;</div><div class="line">		System.out.println(note+<span class="string">"["</span>+bs.length+<span class="string">"]: "</span>+Coder.encode(bs));</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="RSA-java"><a href="#RSA-java" class="headerlink" title="RSA.java"></a>RSA.java</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.security.KeyPair;</div><div class="line"><span class="keyword">import</span> java.security.KeyPairGenerator;</div><div class="line"><span class="keyword">import</span> java.security.PrivateKey;</div><div class="line"><span class="keyword">import</span> java.security.PublicKey;</div><div class="line"><span class="keyword">import</span> javax.crypto.Cipher;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestRSA</span> </span>&#123;</div><div class="line">	Cipher cipher;</div><div class="line">	</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="title">TestRSA</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">		cipher=Cipher.getInstance(<span class="string">"RSA"</span>);<span class="comment">//java默认"RSA"="RSA/ECB/PKCS1Padding"  </span></div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="comment">//生成密钥对  </span></div><div class="line">    <span class="function"><span class="keyword">public</span>  KeyPair <span class="title">genKeyPair</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;  </div><div class="line">        KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance(<span class="string">"RSA"</span>);  </div><div class="line">        keyPairGenerator.initialize(<span class="number">1024</span>);        </div><div class="line">        <span class="keyword">return</span> keyPairGenerator.generateKeyPair();  </div><div class="line">    &#125;  </div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getKeyString</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    	KeyPair k = genKeyPair();</div><div class="line">    	</div><div class="line">    	String kstr = <span class="string">""</span>;</div><div class="line">    	</div><div class="line">    	<span class="keyword">return</span> kstr;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">//公钥加密  </span></div><div class="line">    <span class="keyword">public</span>  <span class="keyword">byte</span>[] encrypt(<span class="keyword">byte</span>[] content, PublicKey pKey) <span class="keyword">throws</span> Exception&#123;  </div><div class="line">        cipher.init(Cipher.ENCRYPT_MODE, pKey);  </div><div class="line">        <span class="keyword">return</span> cipher.doFinal(content);  </div><div class="line">    &#125;  </div><div class="line">    <span class="comment">//私钥解密  </span></div><div class="line">    <span class="keyword">public</span>  <span class="keyword">byte</span>[] decrypt(<span class="keyword">byte</span>[] content, PrivateKey sKey) <span class="keyword">throws</span> Exception&#123;  </div><div class="line">        cipher.init(Cipher.DECRYPT_MODE, sKey);  </div><div class="line">        <span class="keyword">return</span> cipher.doFinal(content);  </div><div class="line">    &#125;  </div><div class="line"> </div><div class="line">    <span class="comment">//私钥加密  </span></div><div class="line">    <span class="keyword">public</span>  <span class="keyword">byte</span>[] encrypt(<span class="keyword">byte</span>[] content, PrivateKey sKey) <span class="keyword">throws</span> Exception&#123;  </div><div class="line">        cipher.init(Cipher.ENCRYPT_MODE, sKey);  </div><div class="line">        <span class="keyword">return</span> cipher.doFinal(content);  </div><div class="line">    &#125;  </div><div class="line">    <span class="comment">//公钥解密  </span></div><div class="line">    <span class="keyword">public</span>  <span class="keyword">byte</span>[] decrypt(<span class="keyword">byte</span>[] content, PublicKey pKey) <span class="keyword">throws</span> Exception&#123;  </div><div class="line">        cipher.init(Cipher.DECRYPT_MODE, pKey);  </div><div class="line">        <span class="keyword">return</span> cipher.doFinal(content);  </div><div class="line">    &#125;  </div><div class="line"> </div><div class="line">    </div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">		<span class="keyword">try</span> &#123;</div><div class="line">			TestRSA t = <span class="keyword">new</span> TestRSA();</div><div class="line">			</div><div class="line">			<span class="comment">//密钥对</span></div><div class="line">			KeyPair k = t.genKeyPair();</div><div class="line">			PrivateKey ks = k.getPrivate();</div><div class="line">			PublicKey kp = k.getPublic();</div><div class="line">			</div><div class="line">			t.print(<span class="string">"Private Key"</span>,ks.getEncoded());</div><div class="line">			t.print(<span class="string">"Public Key"</span>,kp.getEncoded());</div><div class="line">			</div><div class="line">			String msg=<span class="string">"abcd"</span>;</div><div class="line">			<span class="keyword">byte</span>[] msgb = msg.getBytes();</div><div class="line">			</div><div class="line">			<span class="keyword">byte</span>[] emsgb = t.encrypt(msgb,ks);</div><div class="line">			<span class="keyword">byte</span>[] dmsgb = t.decrypt(emsgb, kp);</div><div class="line">			</div><div class="line">			t.print(<span class="string">"msgb"</span>, msgb);</div><div class="line">			t.print(<span class="string">"emsgb"</span>, emsgb);</div><div class="line">			t.print(<span class="string">"dmsgb"</span>, dmsgb);</div><div class="line">			</div><div class="line">			emsgb = t.encrypt(msgb,kp);</div><div class="line">			dmsgb = t.decrypt(emsgb, ks);</div><div class="line">			</div><div class="line">			t.print(<span class="string">"msgb"</span>, msgb);</div><div class="line">			t.print(<span class="string">"emsgb"</span>, emsgb);</div><div class="line">			t.print(<span class="string">"dmsgb"</span>, dmsgb);</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">catch</span>(Exception ec) &#123;</div><div class="line">			ec.printStackTrace();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">(String note, <span class="keyword">byte</span>[] bs)</span></span>&#123;</div><div class="line">		System.out.println(note+<span class="string">"["</span>+bs.length+<span class="string">"]: "</span>+Coder.encode(bs));</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-01-13T15:34:10.283Z" itemprop="dateUpdated">2020-01-13 23:34:10</time>
</span><br>


        
        谢谢浏览，我会继续努力的，示例：<a href="/2018/09/17/密码算法实验/" target="_blank" rel="external">http://yoursite.com/2018/09/17/密码算法实验/</a>
        
    </div>
    <footer>
        <a href="http://yoursite.com">
            <img src="/img/avatar.jpg" alt="BingZhenhuang">
            BingZhenhuang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/AES/">AES</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/HASH/">HASH</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/RSA/">RSA</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/网络安全/">网络安全</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2018/09/17/密码算法实验/&title=《密码算法实验》 — 小方块 - hhardyy.com&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2018/09/17/密码算法实验/&title=《密码算法实验》 — 小方块 - hhardyy.com&source=很久很久很久，简直久以前的密码算法实验。。。
" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2018/09/17/密码算法实验/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《密码算法实验》 — 小方块 - hhardyy.com&url=http://yoursite.com/2018/09/17/密码算法实验/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2018/09/17/密码算法实验/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2018/09/21/redux、react-router/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">redux、react-router</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2018/09/17/Vue音乐播放器/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Vue音乐播放器</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        🤠 请我喝可乐！
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content" style="width:50%">
        
        <div class="reward-code" style="text-align:center">
            <div style="width:300px;margin:0px auto;">
               <img id="rewardCode" style="width:50%;height:60%;display:block; margin:0px auto;" src="/img/alipay.jpg" alt="支付宝打赏二维码">
               <span style="display:inline-block; margin-bottom:20px;">0.88(支付宝 aliPay)</span>
               <img id="rewardCode" style="width:50%;height:60%;display:block; margin:0px auto;" src="/img/wechat.jpg" alt="微信打赏二维码">
               <span style="display:inline-block;">0.88(微信 weChat)</span>
            </div>
        </div>
    </div>
</div>



</div>

        <script>
!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
</script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script>
    function secondToDate(second) {
        if (!second) {
            return 0;
        }
        var time = new Array(0, 0, 0, 0, 0);
        if (second >= 365 * 24 * 3600) {
            time[0] = parseInt(second / (365 * 24 * 3600));
            second %= 365 * 24 * 3600;
        }
        if (second >= 24 * 3600) {
            time[1] = parseInt(second / (24 * 3600));
            second %= 24 * 3600;
        }
        if (second >= 3600) {
            time[2] = parseInt(second / 3600);
            second %= 3600;
        }
        if (second >= 60) {
            time[3] = parseInt(second / 60);
            second %= 60;
        }
        if (second > 0) {
            time[4] = second;
        }
        return time;
    }</script>
<script type="text/javascript" language="javascript">
    function setTime() {
        var create_time = Math.round(new Date(Date.UTC(2017, 08, 18, 11, 42, 23)).getTime() / 1000);
        var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
        currentTime = secondToDate((timestamp - create_time));
        currentTimeHtml = 'Running：' + currentTime[0] + '年 ' + currentTime[1] + '天 '
                + currentTime[2] + '时 ' + currentTime[3] + '分 ' + currentTime[4]
                + '秒';
        document.getElementById("htmer_time").innerHTML = currentTimeHtml;
    }    setInterval(setTime, 1000);
</script>
<footer class="footer">
    <div class="top">
        

        <p>
          <span id="busuanzi_container_page_pv">
             [&nbsp;浏览量：&nbsp;<span id="busuanzi_value_page_pv"></span>&nbsp;]
          </span>
        </p>
    </div>
    <div class="bottom">
        <p>
        <span>BingZhenhuang &copy; 2017 - 2020</span>
            <span>
                
                Power by <a href="https://hhardyy.github.io/" target="_blank">zhen On August 8</a> 
            </span>
            <span id="htmer_time" "></span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2018/09/17/密码算法实验/&title=《密码算法实验》 — 小方块 - hhardyy.com&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2018/09/17/密码算法实验/&title=《密码算法实验》 — 小方块 - hhardyy.com&source=很久很久很久，简直久以前的密码算法实验。。。
" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2018/09/17/密码算法实验/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《密码算法实验》 — 小方块 - hhardyy.com&url=http://yoursite.com/2018/09/17/密码算法实验/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2018/09/17/密码算法实验/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>








<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '(•‾̑⌣‾̑•)✧˖°回来看我';
            clearTimeout(titleTime);
        } else {
            document.title = '(゜-゜)つロ欢迎回来';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
